#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "usr_cfg.h"
#include "afe_init.h"
#include "led_gpio_ctl.h"
#include "spi_dev_ctl.h"
#include "afe_cmd.h"
#include "zlog.h"


void afe_spi_default_config_init(sys_cfg_info *sys, afe_dev_info *afe_dev)
{
	uint16_t chx_num = 0;
	uint16_t spdx_num = 0;

	// 设置SPI工作模式0
	system("spi-config -d /dev/spidev1.0 -m 0");
	usleep(1000);
	afe_xc7a_gpio_reset();

	afe1_spi_init(sys, afe_dev->spi);

	sleep(6);
	afe_acq_begin(afe_dev);								// 自检完成后，A14V,A5V上电，时钟使能
	usleep(20000);

	// 设置采样率
	afe_set_samp_freq(afe_dev, SAMP_102K4);
	usleep(1000);

	for (uint16_t i = 0; i < ACC_CH_TOTAL; i++)
	{
		chx_num = 1 << i;
		afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_SAFE);
		usleep(1000);
		afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_DIV_1);
		usleep(1000);
	}

	//for (uint16_t j = 0; j < SPD_CH_TOTAL; j++)
	for (uint16_t j = 0; j < 1; j++)	//因为转速模式硬件上是统一的，所以只采用0通道的配置
	{
		spdx_num = 1 << j;
		afe_set_spdx_mode(afe_dev, spdx_num, SPDX_MODE_SAFE);
		usleep(1000);
	}
	usleep(1000);
}

void afe_mode_config(sys_cfg_info *sys, afe_dev_info *afe_dev, 
							chx_cfg_info chx[], spd_cfg_info spd[])
{
	uint16_t chx_num = 0;
	uint16_t spdx_num = 0;
	int ret;

	// 设置采样率
	switch (sys->samp_freq) 
	{
		case 51200 :
			ret = afe_set_samp_freq(afe_dev, SAMP_51K2);
			break;
		case 102400 :
			ret = afe_set_samp_freq(afe_dev, SAMP_102K4);
			break;
		case 204800 :
			ret = afe_set_samp_freq(afe_dev, SAMP_204K8);
			break;
		case 409600 :
			ret = afe_set_samp_freq(afe_dev, SAMP_409K6);
			break;
		default :
			ret = afe_set_samp_freq(afe_dev, SAMP_102K4);
			break;
	}
	usleep(1000);
	if (ret < 0) {
		printf("afe_mode_config afe_set_samp_freq error %d!\n", ret);
		dzlog_error("afe_mode_config afe_set_samp_freq error %d!", ret);
		exit(EXIT_FAILURE);
	}

	for (uint16_t i = 0; i < ACC_CH_TOTAL; i++)
	{
		chx_num = 1 << i;
		// 设置工作模式
		switch (chx[i].chx_mode)
		{
			case SAFE :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_SAFE);
				break;
			case IEPE :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_IEPE);
				break;
			case AC :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_AC);
				break;
			case DC :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_DC);
				break;
			case TIB :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_TIB);
				break;
			case CAL :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_CAL);
				break;
			default :
				ret = afe_set_chx_mode(afe_dev, chx_num, CHX_MODE_SAFE);
				break;
		}
		if (ret < 0) {
			printf("afe_mode_config afe_set_chx_mode error %d!\n", ret);
			dzlog_error("afe_mode_config afe_set_chx_mode error %d!", ret);
			exit(EXIT_FAILURE);
		}

		
		// 设置增益
		if (chx[i].chx_pga_gain == 0.125)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_DIV_8);
		else if (chx[i].chx_pga_gain == 0.25)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_DIV_4);
		else if (chx[i].chx_pga_gain == 0.5)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_DIV_2);
		else if (chx[i].chx_pga_gain == 1.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_DIV_1);
		else if (chx[i].chx_pga_gain == 2.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_2);
		else if (chx[i].chx_pga_gain == 4.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_4);
		else if (chx[i].chx_pga_gain == 8.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_8);
		else if (chx[i].chx_pga_gain == 16.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_16);
		else if (chx[i].chx_pga_gain == 32.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_32);
		else if (chx[i].chx_pga_gain == 64.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_64);
		else if (chx[i].chx_pga_gain == 128.0)
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_MUL_128);
		else
			ret = afe_set_chx_gain(afe_dev, chx_num, PGA_GAIN_DIV_1);
		usleep(1000);
		if (ret < 0) {
			printf("afe_mode_config afe_set_chx_gain error %d!\n", ret);
			dzlog_error("afe_mode_config afe_set_chx_gain error %d!", ret);
			exit(EXIT_FAILURE);
		}

	}
	
	// for (uint16_t j = 0; j < SPD_CH_TOTAL; j++)
	for (uint16_t j = 0; j < 1; j++)	//因为转速模式硬件上是统一的，所以只采用0通道的配置
	{
		spdx_num = 1 << j;

	// 设置工作模式
		switch (spd[j].spd_mode)
		{
			case CLOSE :
				ret = afe_set_spdx_mode(afe_dev, spdx_num, SPDX_MODE_SAFE);
				break;
			case PHOTO :
				ret = afe_set_spdx_mode(afe_dev, spdx_num, SPDX_MODE_OPT);
				break;
			case MAGN :
				ret = afe_set_spdx_mode(afe_dev, spdx_num, SPDX_MODE_MAG);
				break;
			case PLUSE :
				ret = afe_set_spdx_mode(afe_dev, spdx_num, SPDX_MODE_TIB);
				break;
			default :
				ret = afe_set_spdx_mode(afe_dev, spdx_num, SPDX_MODE_SAFE);
				break;
		}
		usleep(1000);
		if (ret < 0) {
			printf("afe_mode_config afe_set_spdx_mode error %d!\n", ret);
			dzlog_error("afe_mode_config afe_set_spdx_mode error %d!", ret);
			exit(EXIT_FAILURE);
		}
	}
	usleep(1000);
}


